class Solution:
    def decodeString(self, s: str) -> str:
        stack = []
        nowNum = ""
        r = ""
        for i in s:
            if '0'<=i<='9':
                nowNum += i
            elif i=='[':
                stack.append(nowNum)
                stack.append('[')
                nowNum = ''
            elif i==']':
                tmp = ""
                k = stack.pop(-1)
                while k != '[':
                    tmp = k + tmp
                    k = stack.pop(-1)
                stack.append(int(stack.pop(-1)) * tmp)
            else:
                stack.append(i)
        return "".join(stack)

if __name__ == '__main__':
    so = Solution()
    s = "2[abc]3[cd]ef"
    r = so.decodeString(s)
    print(r)

